package com.dominator.shiro;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.Dtos;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.dao.RedisUtil;
import com.dominator.utils.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.filter.authc.AnonymousFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gsh
 */
@Slf4j
public class ApiFilter extends AnonymousFilter {

    private static RedisUtil ru = RedisUtil.getRu();


    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue)  {
        ApiUtils.checkFixTime(response);
        return authfilter(request,response);
    }

    /**
     * api过滤方法
     * @param request
     * @param response
     * @return
     */
    public boolean authfilter(ServletRequest request,ServletResponse response)  {
        HttpServletRequest req = (HttpServletRequest) request;
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String reqUrl = req.getRequestURI();
        //是否忽略
        boolean res = ignoreUrl(reqUrl);
        if (res){
            return true;
        }
        Enumeration<String> headerNames=req.getHeaderNames();

        for(Enumeration<String> e = headerNames; e.hasMoreElements();){

            String thisName=e.nextElement().toString();

            String thisValue=req.getHeader(thisName);

            log.info("header的key:"+thisName+"--------------header的value:"+thisValue);

        }


        String phoneNum=req.getHeader("phone");
        String token=req.getHeader("token");


        if (SystemUtils.isNotEmpty(phoneNum)&&SystemUtils.isNotEmpty(token)){
            try {
                ApiUtils.checkToken(phoneNum,token);
            }catch (ApiException e){
                try {
                    response.getWriter().print(ReqEnums.REQ_TOKEN_FALSE.toString());
                } catch (IOException e1) {
                    e1.printStackTrace();
                    return false;
                }
                return false;
            }
        }else {
            try {
                response.getWriter().print(ReqEnums.REQ_TOKEN_FALSE.toString());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return false;
        }
        return true;
    }


    /**
     * 忽略url
     *
     * @param url
     * @return
     */
    private static  boolean ignoreUrl(String url) {
        boolean res =false;
        String[] urls= url.split("\\/");
        for(String uri :urls){
            for (String reg:regEx){
                 res =uri.equals(reg);
                if (res){
                    break;
                }
            }
            if (res){
                break;
            }

        }
        return res;
    }

    private static String[] regEx = new String[]{"login","sms","common","merchant","wc2018","lottery","machine","game","guaMachine"};

    public static void main(String[] args) {
        Date bdate =new Date();
        System.out.println(ApiFilter.ignoreUrl("/fenful/api/v1/user/editRealName"));
        long res= new Date().getTime()-bdate.getTime();
        System.out.println("时间"+res);
    }

}
